#!/usr/bin/env python
# -*- encoding: utf-8 *-*
#
# Focalist - a passive time track tools
#
# Copyright (c) 2008 Osvaldo Santana Neto <osantana@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

from cStringIO import StringIO
from configobj import ConfigObj

GLOBAL_CONFIG = "/etc/focalist.cfg"
INITIAL_CONFIG = """[settings]

[rules]
[[Mozilla Firefox]]
pattern = (?P<l2>.*) - (?P<l1>Mozilla Firefox)
"""

class _Configuration(object):
    def __init__(self):
        self.user_conf = os.path.expanduser("~/.focalist.cfg")

        if os.path.exists(self.user_conf):
            self.config = ConfigObj(self.user_conf)
        elif os.path.exists(GLOBAL_CONFIG):
            self.config = ConfigObj(GLOBAL_CONFIG)
        else:
            self.config = ConfigObj(INITIAL_CONFIG.split(os.linesep))
            self.config.filename = self.user_conf
            self.config.write()

    @property
    def rules(self):
        return self.config.get('rules', {})

    def add_rule(self, name, pattern):
        self.config['rules'][name] = {'pattern': pattern}
        self.config.write()

    def remove_rule(self, name):
        pass

config = _Configuration()

# vim:ts=4:tw=120:sm:et:si:ai

